using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoadTree_Ctrl : MonoBehaviour
{

    [SerializeField]
    public float scaleSpeed;
    public bool isLocal;
    public Vector3 startPoint;
    public Vector3 endPoint;
    private Vector3 _startPoint
    {
        get
        {
            return isLocal ? this.transform.position + startPoint : startPoint;
        }
    }
    private Vector3 _endPoint
    {
        get
        {
                return isLocal ? this.transform.position + endPoint : endPoint;
        }
    }
    public Vector3 direction
    {
        get
        {
            return endPoint - startPoint;
        }
    }

    public GameObject tree_Prefab;
    public float initSize = 0;
    public float targetSize = 3;
    public float spawnIntervalTime = 0.5f;
    public float spawnTime = 0;
    public float aliveTimeCount = 4;
    public AnimationCurve moveCurve;
    private Vector2 initialScale; //物体初始缩放比例
    public int currentSortingOrder;
    public Color color = Color.white;
    public float sceneTimeScale = 1;
    public IEPool_Manager.DeltaTimeData deltaTimeData=new IEPool_Manager.DeltaTimeData(0);
    private void OnDrawGizmosSelected()
    {
        Gizmos.DrawLine(_startPoint, _endPoint);
    }
    void Start(){

        initialScale = transform.localScale;
       // transform.position = startPoint;
    }
    private void OnDestroy()
    {

    }
    void Update()
    {
        deltaTimeData.deltaTime = GameManager.instance.deltaTimeData.deltaTime * sceneTimeScale;
        //this.transform.position += direction * Time.deltaTime;
        spawnTime += deltaTimeData.deltaTime;
        if (spawnTime >= spawnIntervalTime)
        {
            spawnTime = 0;
            GameObject tree = ObjectPool_Manager.instance.Create(tree_Prefab.GetInstanceID() + "", tree_Prefab, this.transform);
            currentSortingOrder--;
            SpriteRenderer spriteRenderer = tree.GetComponent<SpriteRenderer>();
            spriteRenderer.color = color;
            spriteRenderer.sortingOrder = currentSortingOrder;
            tree.transform.position = _startPoint;
            tree.transform.localScale = Vector3.zero;
            IEPool_Manager.instance.KeepTimeToDo("", aliveTimeCount, deltaTimeData, (_time) =>
            {
                tree.transform.position = Vector3.Lerp(_startPoint, _endPoint, moveCurve.Evaluate(1 - _time / aliveTimeCount));
                tree.transform.localScale = Vector3.one * Mathf.Lerp(initSize, targetSize, 1 - _time / aliveTimeCount);
                return true;
            }, () =>
            {
                ObjectPool_Manager.instance.Hide(tree_Prefab.GetInstanceID() + "", tree);
            },1);
        }
    }
}
